package com.hfplm.hsdz.util;

import java.util.Date;
import com.teamcenter.rac.kernel.TCComponentItem;
import com.teamcenter.rac.kernel.TCComponentItemRevision;
import com.teamcenter.rac.kernel.TCException;


public class HSUtil {
	private HSUtil() {
	}
	private static class lvUtilsHolder     
    {     
        public final static HSUtil instance = new HSUtil();     
    }     
    
    public static HSUtil getInstance()     
    {  
        return lvUtilsHolder.instance;     
    }
    
	 public TCComponentItemRevision getLatestReleasedRevision(TCComponentItem item)throws TCException
		{
			TCComponentItemRevision[] revisions = item.getReleasedItemRevisions();
			if(revisions == null || revisions.length == 0)
				return null;
			
			if(revisions.length == 1)
				return revisions[0];
			
			Date latestDate = null;
			TCComponentItemRevision latestRevision = null;
			
			for(int i = 0 ; i < revisions.length; i++)
			{
				Date releasedDate = revisions[i].getTCProperty("date_released").getDateValue();
				if(latestDate == null)
				{
					latestDate = releasedDate;
					latestRevision = revisions[i];
				}
				else
				{
					if(releasedDate.after(latestDate))
					{
						latestDate = releasedDate;
						latestRevision = revisions[i];
					}
				}
			}
			
			return latestRevision;
		}
}
